import { getDishesInfoData, getBoardData, getBusinessList, getTradingList, getTradingDetail } from '@/api/operating'
import { getTopProductTypeList } from '@/api'
import * as types from '../../mutation-types'
import { processPntAndSat } from '@/utils'
export default {
  namespaced: true, // 封闭
  state: {
    topProductTypeData: [],
    marketInfoData: [],
    marketInfoTotal: 0
  },
  mutations: {
    [types.SET_TOP_PRODUCT_TYPE_DATA](state, data) {
      state.topProductTypeData = data
    },
    [types.SET_MARKET_INFO_DATA](state, data) {
      state.marketInfoData = data
    },
    [types.SET_MARKET_INFO_PAGE](state, total) {
      state.marketInfoTotal = total
    }
  },
  getters: {
    filterMarketInfoData(state) {
      return state.marketInfoData.map(item => {
        return {
          productName: item.productName,
          productTypeName: item.productTypeName,
          avgPrice: (item.avgPrice === '-' ? item.avgPrice : (item.avgPrice / 100)).toFixed(2),
          prevAvgPrice: (item.prevAvgPrice === '-' ? item.prevAvgPrice : (item.prevAvgPrice / 100)).toFixed(2),
          avgPriceTrend: processPntAndSat(item.avgPriceTrend),
          totalWeight: (item.totalWeight === '-' ? item.totalWeight : (item.totalWeight / 500)),
          prevTotalWeight: (item.prevTotalWeight === '-' ? item.prevTotalWeight : (item.prevTotalWeight / 500)),
          totalWeightTrend: processPntAndSat(item.totalWeightTrend)
        }
      })
    }
  },
  actions: {
    /** 菜品统计 */
    // 获取商品顶级分类列表
    async fetchTopProductTypeList(context, payload) {
      try {
        const { data } = await getTopProductTypeList()
        for (let i = 0; i < data.length; i++) {
          data[i].id = data[i].id + ''
        }
        context.commit(types.SET_TOP_PRODUCT_TYPE_DATA, data)
      } catch (err) {
        console.log(err)
      }
    },
    // 获取菜品统计列表
    async fetchDishesInfoData(context, payload) {
      const { dateType, topTypeId, perPage, page } = payload
      try {
        const { data } = await getDishesInfoData(dateType, topTypeId, perPage, page)
        context.commit(types.SET_MARKET_INFO_DATA, data.items)
        context.commit(types.SET_MARKET_INFO_PAGE, data.total)
      } catch (err) {
        console.log(err)
        context.commit(types.SET_MARKET_INFO_DATA, [])
      }
    },

    /** 商户统计 */
    // 获取经营数据看板
    async fetchBoardData(context, payload) {
      try {
        const { data } = await getBoardData()
        return data
      } catch (err) {
        console.log(err)
      }
    },

    // 获取商户统计列表
    async fetchBusinessData(context, payload) {
      const {
        ids, businessTypes, payWays, startTime, endTime, page, size
      } = payload
      try {
        const { data } = await getBusinessList(
          ids, businessTypes, payWays, startTime, endTime, page, size
        )
        return data
      } catch (err) {
        console.log(err)
      }
    },

    // 获取交易列表
    async fetchTradingData(context, payload) {
      const {
        marketId, businessId, code, startDate, endDate, payWay, perPage, page
      } = payload
      try {
        const { Data } = await getTradingList(
          marketId, businessId, code, startDate, endDate, payWay, perPage, page
        )
        return Data
      } catch (err) {
        console.log(err)
      }
    },

    // 获取交易详情
    async fetchTradingDetail(context, payload) {
      const { transactionId } = payload
      try {
        const { data } = await getTradingDetail(
          transactionId
        )
        return data
      } catch (err) {
        console.log(err)
      }
    }
  }
}
